package cn.cookyfly.dailyreview.server.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

public class PropertiesDef {

	private static final String PROPERTIES_FILE_NAME = "definition.properties";
	private static final String DEFAULT_DB_HOST = "localhost";
	private static final String DEFAULT_DB_PORT = "3306";
	
	private static final Logger logger = Logger.getLogger(PropertiesDef.class.getName());
	
	private static String dbHost;
	private static String dbPort;
	private static String defaultDatabase;
	private static String loginId;
	private static String loginPasswd;
	
	static {// load all the properties values from the .properties file
		loadProperties();
	}

	public static boolean loadProperties() {
		Properties props = new Properties();
		try {
			InputStream is = PropertiesDef.class.getResourceAsStream(PROPERTIES_FILE_NAME);
			if (is == null) {
				throw new IOException("File not found: " + PROPERTIES_FILE_NAME);
			}
			props.load(is);
			dbHost = props.getProperty("dbHost", DEFAULT_DB_HOST);
			dbPort = props.getProperty("dbPort", DEFAULT_DB_PORT);
			defaultDatabase = props.getProperty("defaultDatabase");
			loginId = props.getProperty("loginId");
			loginPasswd = props.getProperty("loginPasswd");
		} catch (IOException e) {
			logger.severe("Loaing properties failed due to IOException! Exception message: " + 
					e.getMessage());
			return false;
		}
		return true;
	}
	
	public static String getDbHost() {
		return dbHost;
	}

	public static String getDbPort() {
		return dbPort;
	}

	public static String getDefaultDatabase() {
		return defaultDatabase;
	}

	public static String getLoginId() {
		return loginId;
	}

	public static String getLoginPasswd() {
		return loginPasswd;
	}
	
}
